﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Xml.Linq;

namespace devtm.Editor.TypeSelector
{
    public class ValueParameter : INotifyPropertyChanged
    {

        private ObjectContainer _parent;

        public ValueParameter(ObjectContainer parent)
        {
            Key = Guid.NewGuid();
            this._parent = parent;
        }
        public Guid Key { get; set; }

        private Parameter _param;

        public string Name { get { return MyParameter == null ? string.Empty : MyParameter.Name; } }


        public Parameter MyParameter
        {
            get { return _param; }
            set
            {
                if (_param != null && value != null && _param.Name == value.Name)
                    return;

                foreach (ValueParameter item in _parent.ValuesParameters)
                {
                    if (item.MyParameter != null && value != null && item.MyParameter.Name == value.Name)
                        return;
                }

                _param = value;

                Type = value.Type;

                MyPropertyChanged("Name");
                MyPropertyChanged("MyParameter");
                MyPropertyChanged("ViewCombo");
                MyPropertyChanged("ViewText");

                MyPropertyChanged("TextValueVisible");
                MyPropertyChanged("ComboValueVisible");


                SetComboValues();


                if (_param == null)
                    return;

                if (string.IsNullOrEmpty(_param.Name) || _parent.ValuesParameters.Count >= _parent.Parameters.Count)
                    return;

                _parent.ValuesParameters.AddParameter();



            }
        }

        private void SetComboValues()
        {
            _comboItemsSource = new List<Object>();

            if (Type == typeof(Type))
            {

            }

            else if (Type.IsEnum)
            {
                foreach (string item in Enum.GetNames(Type))
                    _comboItemsSource.Add(item);
            }

            else if (Type == typeof(bool))
            {
                _comboItemsSource.Add("True");
                _comboItemsSource.Add("False");
            }

            MyPropertyChanged("ComboItemsSource");
        }


        public Object Value { get; set; }
        public Type Type { get; set; }

        public List<Parameter> Parameters { get { return _parent.Parameters.Where(c => IsValid(c)).ToList(); } }

        private bool IsValid(Parameter c)
        {

            foreach (ValueParameter item in _parent.ValuesParameters)
                if (item.MyParameter != null && item.MyParameter.Name == c.Name)
                    return false;

            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void MyPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }
        public void MyPropertyChanged(string propertyName)
        {
            MyPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        public Visibility ViewCombo { get { return MyParameter == null ? Visibility.Visible : Visibility.Collapsed; } }
        public Visibility ViewText { get { return MyParameter != null ? Visibility.Visible : Visibility.Collapsed; } }

        public Visibility TextValueVisible
        {
            get
            {

                if (MyParameter == null || Type == typeof(bool))
                    return Visibility.Collapsed;


                if (Type == typeof(string) || Type.IsPrimitive)
                    return Visibility.Visible;

                return Visibility.Collapsed;

            }
        }
        public Visibility ComboValueVisible
        {
            get
            {
                if (MyParameter == null)
                    return Visibility.Collapsed;

                if (Type == typeof(Type) || Type.IsEnum || Type == typeof(bool))
                    return Visibility.Visible;

                return Visibility.Collapsed;

            }
        }

        private List<Object> _comboItemsSource;
        public IEnumerable<Object> ComboItemsSource
        {
            get
            {
                return _comboItemsSource;
            }
        }


        public XElement ToAttribute()
        {

            string Mask = string.Empty;
            
            if (String.IsNullOrEmpty(Name) || Type == null)
                return null;

            string v = string.Empty;

            if (Type == typeof(bool) || Type.IsPrimitive || Type.IsEnum)
                 v = Value.ToString();

            else if (Type == typeof(Type))
                v = (Value as Type).AssemblyQualifiedName;
            
            else
                v = (string)Value;

            return new XElement(Name, v);

        }
    }
}
